package com.yihuu.service.impl;

import com.yihuu.Util.MD5Util;
import com.yihuu.common.Result;
import com.yihuu.entity.User;
import com.yihuu.repository.UserRepository;
import com.yihuu.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * Created by yuhui on 2017/8/15.
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public Result login(String uname, String upwd) {
        User user = userRepository.findByUnameAndUpassword(uname, MD5Util.md5(upwd));
        if(user==null){
            return Result.createByError("用户名或密码错误");
        }else{
            return Result.createBySuccess("登陆成功",user);
        }
    }

    //判断用户名或者手机号是否存在
    @Override
    public Boolean checkColumn(String type, String uname,String uphone) {
        if(StringUtils.isBlank(type)){
            return false;
        }else{
            if(StringUtils.equals(type,"uphone")){
                Long phone = userRepository.findByPhone(uphone);
                if(phone>0){
                    return true;
                }else{
                    return false;
                }
            }
            if(StringUtils.equals(type,"uname")){
                Long u = userRepository.findByUname(uname);
                if(u>0){
                    return true;
                }else{
                    return false;
                }
            }
        }
        return false;
    }

    @Override
    public Result register(User user) {
        user.setUpassword(MD5Util.md5(user.getUpassword()));
        user.setUpdateTime(new Date());
        user.setCreateTime(new Date());
        User user1 = userRepository.save(user);
        if(user1!=null) {
            return Result.createBySuccess("注册成功", user1);
        }else{
            return Result.createByError("注册失败");
        }
    }

    @Override
    public Result getQuestion(String uanme) {
        String question = userRepository.getQuestion(uanme);
        return Result.createBySuccess("获取问题成功",question);
    }

    @Override
    public Result lossPassword(String uname, String question, String answer) {
        User user = userRepository.findByUnameAndQuestionAndAnswer(uname, question,answer);
        if(user!=null){
            return Result.createBySuccess("验证通过",true);
        }else{
            return Result.createBySuccess("验证不通过",false);
        }
    }


    @Override
    public Result lossPassword(String uname, String question,String answer, String upassword) {
        if(StringUtils.isNotBlank("uname")){
            User user = userRepository.findByUnameAndQuestionAndAnswer(uname, question,answer);
            if(user!=null){
                Integer integer = userRepository.updateByUnameAndQuestionAndAndAnswer(user.getUname(), question, answer, new Date(), MD5Util.md5(upassword));
                if(integer>0){
                    return Result.createBySuccess("密码重置成功");
                }else{
                    return Result.createByError("密码重置失败");
                }
            }
        }
        return Result.createByError("信息输入错误");
    }

    @Override
    public Result updateInfo(User user) {
        User one = userRepository.findOne(user.getUid());
        one.setUpdateTime(new Date());
        one.setEmail(user.getEmail());
        one.setUaddr(user.getUaddr());
        one.setUage(user.getUage());
        one.setUdesc(user.getUdesc());
        one.setUlove(user.getUlove());
        one.setUphone(user.getUphone());
        one.setValid(true);
        User user1 = userRepository.save(one);
        return Result.createBySuccess("用户信息保存成功",user1);
    }

    @Override
    public Result setQuestion(User user) {
        User one = userRepository.findOne(user.getUid());
        one.setQuestion(user.getQuestion());
        one.setAnswer(user.getAnswer());
        User save = userRepository.save(one);
        if(save!=null){
            return Result.createBySuccess("密保问题设置成功");
        }else{
            return Result.createByError("密保问题设置失败");
        }
    }
}
